﻿using System;
using System.Data;
using System.IO;
using System.Text;
using System.Text.RegularExpressions;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

/// <summary>
/// Summary description for Businesses
/// </summary>
public class Businesses
{
    public static Exception _FileOperationException; 
    public static string serverPath;
    public Businesses()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public static string WebPath()
    {
        return ConfigurationSettings.AppSettings["DefaultPath"];
    }

    // '' <summary>
    // '' maps the current web path to a server filesystem path
    // '' </summary>
    public static string GetLocalPath(string strFilename)
    {
        return Path.Combine(serverPath, strFilename);
    }

    public static string GetTargetPath(string strFilename) {
        return Path.Combine(GetLocalPath(""), strFilename);
    }
    // '' <summary>
    // '' creates a subfolder in the current folder
    // '' </summary>
    public static void MakeFolder(string strFilename)
    {
        string strLocalPath = GetLocalPath(strFilename);
        try
        {
            if (!Directory.Exists(strLocalPath))
            {
                Directory.CreateDirectory(strLocalPath);
            }
        }
        catch (Exception ex)
        {
            _FileOperationException = ex;
        }
    }
    // '' <summary>
    // '' recursively copies a folder, and all subfolders and files, to a target path
    // '' </summary>
    public static void CopyFolder(string strSourceFolderPath, string strDestinationFolderPath, bool blnOverwrite)
    {
        // -- make sure target folder exists
        if (!Directory.Exists(strDestinationFolderPath))
        {
            Directory.CreateDirectory(strDestinationFolderPath);
        }
        // -- copy all of the files in this folder to the destination folder
        foreach (string strFilePath in Directory.GetFiles(strSourceFolderPath))
        {
            string strFileName = Path.GetFileName(strFilePath);
            // -- if exception, will be caught in calling proc
            File.Copy(strFilePath, Path.Combine(strDestinationFolderPath, strFileName), blnOverwrite);
        }
        // -- copy all of the subfolders in this folder
        foreach (string strFolderPath in Directory.GetDirectories(strSourceFolderPath))
        {
            string strFolderName = Regex.Match(strFolderPath, "[^\\\\]+$").ToString();
            CopyFolder(strFolderPath, Path.Combine(strDestinationFolderPath, strFolderName), blnOverwrite);
        }
    }

    // '' <summary>
    // '' returns True if the provided path is an existing directory
    // '' </summary>
    public static bool IsDirectory(string strFilepath) {
        return Directory.Exists(strFilepath);
    }
    
    // '' <summary>
    // '' moves a file from the current folder to the target folder
    // '' </summary>
    public static void CopyFileOrFolder(string strName) {
        string strLocalPath = GetLocalPath(strName);
        string strTargetPath = GetTargetPath(strName);
        try {
            if (IsDirectory(strLocalPath)) {
                CopyFolder(strLocalPath, strTargetPath, true);
            }
            else {
                File.Copy(strLocalPath, strTargetPath);
            }
        }
        catch (Exception ex) {
            _FileOperationException = ex;
        }
    }
    // '' <summary>
    // '' recursively removes the read only tag from a file or folder, if it is present
    // '' </summary>
    public static void RemoveReadOnly(string strPath)
    {
        if (IsDirectory(strPath))
        {
            foreach (string strFile in Directory.GetFiles(strPath))
            {
                RemoveReadOnly(strFile);
            }
            foreach (string strFolder in Directory.GetDirectories(strPath))
            {
                RemoveReadOnly(strFolder);
            }
        }
        else
        {
            FileInfo fi = new FileInfo(strPath);
            if (fi.IsReadOnly)
            {
                fi.IsReadOnly=false;
                
            }
        }
    }

    // '' <summary>
    // '' deletes a file or folder
    // '' </summary>
    public static void DeleteFileOrFolder(string sPath)
    {
        //string strLocalPath = GetLocalPath(strName);
        try
        {
            RemoveReadOnly(sPath);
            if (IsDirectory(sPath))
            {
                Directory.Delete(sPath, true);
            }
            else
            {
                File.Delete(sPath);
            }
        }
        catch (Exception ex)
        {
            _FileOperationException = ex;
        }
    }
    public static string GetUrlProductImage(object oInput)
    {
        string sInput, sOutput;

        if (oInput == null)
        {
            sInput = "";
        }
        else
        {
            sInput = oInput.ToString().Trim();
        }
        if (sInput == "")
        {
            sOutput = "/images/no_photo.gif";
        }
        else
            if (sInput.IndexOf("http://") == -1)
            {
                sOutput = "/images/" + sInput;
            }
            else
            {
                sOutput = sInput;
            }
        return sOutput;
    }

    public static string GetCompanyName()
    {
        string sqlStr = "select CompanyName from Webvariants";
        DataTable dt = dal.SelectTable(sqlStr);
        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0][0].ToString();
        }
        return string.Empty;
    }
    public static string GetCompanyShortDes()
    {
        string sqlStr = "select CompanyShortDes from Webvariants";
        DataTable dt = dal.SelectTable(sqlStr);
        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0][0].ToString();
        }
        return string.Empty;
    }
    public static string GetVisitors()
    {
        string sqlStr = "select Visitors from Webvariants";
        DataTable dt = dal.SelectTable(sqlStr);
        if (dt.Rows.Count > 0)
        {
            return dt.Rows[0][0].ToString();
        }
        return string.Empty;
    }
}
